package com.powernode.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.constant.RedisConstant;
import com.powernode.dto.PageParam;
import com.powernode.dto.PageResult;
import com.powernode.entity.IndexCarousel;
import com.powernode.exception.BizException;
import com.powernode.mapper.IndexCarouselMapper;
import com.powernode.service.IndexCarouselService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.powernode.util.MinioUtil;
import com.powernode.vo.IndexCarouselVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 首页商品轮播图 服务实现类
 * </p>
 * @CacheConfig 使用缓存类
 * @author Juice
 * @since 2023-01-28
 */
@Service
@CacheConfig(cacheNames = RedisConstant.KEY_INDEX_CAROUSEL)
public class IndexCarouselServiceImpl extends ServiceImpl<IndexCarouselMapper, IndexCarousel> implements IndexCarouselService {

    @Autowired
    private MinioUtil minioUtil;

    @Override
    public PageResult<IndexCarousel> pageQuery(PageParam param) {
        Page<IndexCarousel> page = this.page(new Page<>(param.getPageNumber(), param.getPageSize()));
        return new PageResult<>(page.getTotal(),page.getRecords());
    }

    @Override
    @CacheEvict(allEntries = true)//增删改，都清除当前缓存key的全部缓存，因为查询方法缓存的是一个集合，在更新时不能精确定位到个体
    public boolean saveIndexCarousel(IndexCarousel entity) {
        return this.save(entity);
    }

    @Override
    @CacheEvict(allEntries = true)//增删改，都清除当前缓存key的全部缓存，因为查询方法缓存的是一个集合，在更新时不能精确定位到个体
    public boolean updateIndexCarousel(IndexCarousel entity) {
        return this.updateById(entity);
    }

    @Override
    @CacheEvict(allEntries = true)//增删改，都清除当前缓存key的全部缓存，因为查询方法缓存的是一个集合，在更新时不能精确定位到个体
    public boolean removeIndexCarousel(Long id) {
        IndexCarousel carousel = this.getById(id);
        if (carousel == null) {
            throw new BizException(HttpStatus.BAD_REQUEST.value(), "首页轮播图id不存在");
        }
        String img = carousel.getImg();
        this.removeById(id);
        if (StrUtil.isNotBlank(img)){
            minioUtil.removeFile(img.substring(img.lastIndexOf("/") + 1));
        }
        return true;
    }

    @Override
    @Cacheable
    public List<IndexCarouselVo> listCarouselsForIndex(Integer num) {
        //分页
        LambdaQueryWrapper<IndexCarousel> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(IndexCarousel::getSort);
        Page<IndexCarousel> page = this.page(new Page<>(1, num), wrapper);
        //数据复制到结果集合
        List<IndexCarouselVo> result = new ArrayList();
        page.getRecords().forEach(indexCarousel -> {
            IndexCarouselVo indexCarouselVo = new IndexCarouselVo();
            BeanUtils.copyProperties(indexCarousel,indexCarouselVo);
            result.add(indexCarouselVo);

        });

        return result;
    }
}
